<template>
  <div class="product-detail">
    <div v-if="product">
      <div class="back-button" @click="goBack">
        <i class="fas fa-arrow-left"></i> 返回
      </div>
      
      <div class="product-container">
        <div class="product-gallery">
          <img :src="product.image" :alt="product.name" class="main-image">
          <div class="image-thumbnails" v-if="product.images && product.images.length">
            <img 
              v-for="(img, index) in product.images" 
              :key="index"
              :src="img"
              :alt="`${product.name} - 图片 ${index + 1}`"
              @click="product.image = img"
              class="thumbnail"
            >
          </div>
        </div>

        <div class="product-info">
          <div class="product-header">
            <span class="product-type" :class="product.productType">{{ getProductTypeLabel(product.productType) }}</span>
            <h1>{{ product.name }}</h1>
          </div>

          <div class="product-price">
            <span class="price-label">价格</span>
            <span class="price-value">¥{{ product.price }}</span>
          </div>

          <div class="product-description">
            <h3>产品描述</h3>
            <p>{{ product.description }}</p>
          </div>

          <div class="product-specs" v-if="product.specs">
            <h3>规格参数</h3>
            <div class="specs-grid">
              <div v-for="(value, key) in product.specs" :key="key" class="spec-item">
                <span class="spec-label">{{ key }}</span>
                <span class="spec-value">{{ value }}</span>
              </div>
            </div>
          </div>

          <div class="product-actions">
            <div class="quantity-selector">
              <button @click="decreaseQuantity" :disabled="quantity <= 1">-</button>
              <input type="number" v-model.number="quantity" min="1" max="99">
              <button @click="increaseQuantity" :disabled="quantity >= 99">+</button>
            </div>

            <button 
              @click="addToCart" 
              :disabled="!isLoggedIn"
              :title="!isLoggedIn ? '请先登录' : ''"
              class="add-to-cart-btn"
            >
              <i class="fas fa-shopping-cart"></i>
              加入购物车
            </button>
          </div>
        </div>
      </div>
    </div>
    <div v-else class="loading">
      <p>加载中...</p>
    </div>
  </div>
</template>

<script>
import { ref, computed } from 'vue'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'

export default {
  name: 'ProductDetail',
  setup() {
    const store = useStore()
    const router = useRouter()
    const quantity = ref(1)
    const isLoggedIn = ref(localStorage.getItem('isLoggedIn') === 'true')

    const product = computed(() => {
      const productId = parseInt(router.currentRoute.value.params.id)
      return store.state.products.find(p => p.id === productId)
    })

    const getProductTypeLabel = (type) => {
      const types = {
        motorcycle: '摩托车',
        gear: '骑行装备',
        parts: '配件改装'
      }
      return types[type] || type
    }

    const decreaseQuantity = () => {
      if (quantity.value > 1) quantity.value--
    }

    const increaseQuantity = () => {
      if (quantity.value < 99) quantity.value++
    }

    const addToCart = () => {
      if (!isLoggedIn.value) {
        alert('请先登录')
        return
      }
      store.commit('addToCart', {
        ...product.value,
        quantity: quantity.value
      })
      alert('已添加到购物车')
    }

    const goBack = () => {
      router.back()
    }

    return {
      product,
      quantity,
      isLoggedIn,
      getProductTypeLabel,
      decreaseQuantity,
      increaseQuantity,
      addToCart,
      goBack
    }
  }
}
</script>

<style scoped>
.product-detail {
  padding: 40px;
  max-width: 1200px;
  margin: 0 auto;
  min-height: calc(100vh - 60px);
  background: linear-gradient(135deg, #d4eaf7 0%, #b8d9ee 100%); /* Light cyan-blue gradient */
}

.back-button {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 12px 24px;
  background: linear-gradient(135deg, #0078A0 0%, #005c80 100%); /* Cyan-blue gradient */
  border-radius: 12px;
  cursor: pointer;
  transition: all 0.3s ease;
  margin-bottom: 30px;
  color: white;
  font-weight: 500;
  box-shadow: 0 4px 15px rgba(0, 120, 160, 0.2); /* Cyan-blue shadow */
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.back-button:hover {
  background: linear-gradient(135deg, #005c80 0%, #004060 100%); /* Darker cyan-blue gradient */
  transform: translateX(-5px);
  box-shadow: 0 6px 20px rgba(0, 120, 160, 0.3); /* Cyan-blue shadow */
}

.product-container {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 40px;
  animation: fadeIn 0.6s ease;
  background: rgba(255, 255, 255, 0.95);
  padding: 35px;
  border-radius: 24px;
  box-shadow: 0 15px 40px rgba(0, 120, 160, 0.12); /* Cyan-blue shadow */
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.product-gallery {
  position: relative;
}

.main-image {
  width: 100%;
  height: 450px;
  object-fit: cover;
  border-radius: 20px;
  box-shadow: 0 10px 30px rgba(0, 120, 160, 0.15); /* Cyan-blue shadow */
  transition: all 0.5s ease;
  border: 1px solid rgba(0, 120, 160, 0.1); /* Cyan-blue border */
}

.main-image:hover {
  transform: scale(1.02) translateY(-5px);
  box-shadow: 0 15px 40px rgba(0, 120, 160, 0.2); /* Cyan-blue shadow */
}

.image-thumbnails {
  display: flex;
  gap: 15px;
  margin-top: 20px;
  overflow-x: auto;
  padding: 10px 0;
  scrollbar-width: thin;
  scrollbar-color: #0078A0 #d4eaf7; /* Cyan-blue scrollbar */
}

.image-thumbnails::-webkit-scrollbar {
  height: 6px;
}

.image-thumbnails::-webkit-scrollbar-track {
  background: #d4eaf7; /* Light cyan-blue track */
  border-radius: 10px;
}

.image-thumbnails::-webkit-scrollbar-thumb {
  background: #0078A0; /* Cyan-blue thumb */
  border-radius: 10px;
}

.thumbnail {
  width: 90px;
  height: 90px;
  object-fit: cover;
  border-radius: 16px;
  cursor: pointer;
  transition: all 0.3s ease;
  border: 2px solid transparent;
  box-shadow: 0 4px 12px rgba(0, 120, 160, 0.1); /* Cyan-blue shadow */
}

.thumbnail:hover {
  transform: translateY(-3px) scale(1.05);
  border-color: #0078A0; /* Cyan-blue border */
  box-shadow: 0 6px 15px rgba(0, 120, 160, 0.2); /* Cyan-blue shadow */
}

.product-info {
  display: flex;
  flex-direction: column;
  gap: 25px;
}

.product-header {
  margin-bottom: 25px;
  padding-bottom: 20px;
  border-bottom: 2px solid rgba(0, 120, 160, 0.1); /* Cyan-blue border */
}

.product-type {
  display: inline-block;
  padding: 8px 20px;
  border-radius: 30px;
  font-size: 0.95em;
  font-weight: 600;
  margin-bottom: 15px;
  text-transform: uppercase;
  letter-spacing: 1px;
  box-shadow: 0 4px 12px rgba(0, 120, 160, 0.15); /* Cyan-blue shadow */
}

.product-type.motorcycle {
  background: linear-gradient(135deg, #0078A0 0%, #005c80 100%); /* Cyan-blue gradient */
  color: white;
}

.product-type.gear {
  background: linear-gradient(135deg, #0090B8 0%, #0078A0 100%); /* Lighter cyan-blue gradient */
  color: white;
}

.product-type.parts {
  background: linear-gradient(135deg, #00A0C8 0%, #0090B8 100%); /* Even lighter cyan-blue gradient */
  color: white;
}

.product-header h1 {
  font-size: 2.4em;
  background: linear-gradient(135deg, #005c80 0%, #0078A0 100%); /* Dark cyan-blue to main cyan-blue gradient */
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  margin: 10px 0;
  line-height: 1.2;
  font-weight: 800;
}

.product-price {
  background: linear-gradient(135deg, #d4eaf7 0%, #b8d9ee 100%); /* Light cyan-blue gradient */
  padding: 25px;
  border-radius: 20px;
  display: flex;
  align-items: center;
  gap: 15px;
  border: 1px solid rgba(0, 120, 160, 0.1); /* Cyan-blue border */
  box-shadow: 0 8px 20px rgba(0, 120, 160, 0.08); /* Cyan-blue shadow */
}

.price-label {
  font-size: 1.2em;
  color: #0078A0; /* Cyan-blue */
  font-weight: 600;
}

.price-value {
  font-size: 2.4em;
  background: linear-gradient(135deg, #0078A0 0%, #005c80 100%); /* Cyan-blue gradient */
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  font-weight: 800;
}

.product-description {
  line-height: 1.8;
  color: #4a4a4a;
  font-size: 1.05em;
  padding: 20px;
  background: rgba(212, 234, 247, 0.5); /* Light cyan-blue translucent */
  border-radius: 20px;
  margin: 20px 0;
  border: 1px solid rgba(0, 120, 160, 0.1); /* Cyan-blue border */
}

.product-description h3 {
  color: #005c80; /* Dark Cyan-blue */
  margin-bottom: 15px;
  font-size: 1.4em;
  font-weight: 700;
}

.product-specs {
  background: linear-gradient(135deg, #d4eaf7 0%, #b8d9ee 100%); /* Light cyan-blue gradient */
  padding: 30px;
  border-radius: 20px;
  border: 1px solid rgba(0, 120, 160, 0.1); /* Cyan-blue border */
  box-shadow: 0 8px 25px rgba(0, 120, 160, 0.08); /* Cyan-blue shadow */
}

.product-specs h3 {
  color: #005c80; /* Dark Cyan-blue */
  margin-bottom: 25px;
  font-size: 1.4em;
  font-weight: 700;
  border-bottom: 2px solid rgba(0, 120, 160, 0.1); /* Cyan-blue border */
  padding-bottom: 10px;
}

.specs-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 20px;
}

.spec-item {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 20px;
  background: rgba(255, 255, 255, 0.9);
  border-radius: 16px;
  box-shadow: 0 4px 12px rgba(0, 120, 160, 0.08); /* Cyan-blue shadow */
  transition: all 0.3s ease;
  border: 1px solid rgba(0, 120, 160, 0.1); /* Cyan-blue border */
}

.spec-item:hover {
  transform: translateY(-3px);
  box-shadow: 0 6px 15px rgba(0, 120, 160, 0.12); /* Cyan-blue shadow */
}

.spec-label {
  color: #666;
  font-size: 0.95em;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.spec-value {
  color: #005c80; /* Dark Cyan-blue */
  font-weight: 700;
  font-size: 1.1em;
}

.product-actions {
  display: flex;
  gap: 20px;
  margin-top: 30px;
  padding: 20px;
  background: rgba(212, 234, 247, 0.5); /* Light cyan-blue translucent */
  border-radius: 20px;
  border: 1px solid rgba(0, 120, 160, 0.1); /* Cyan-blue border */
}

.quantity-selector {
  display: flex;
  align-items: center;
  border: 2px solid #0078A0; /* Cyan-blue border */
  border-radius: 15px;
  overflow: hidden;
  background: white;
  box-shadow: 0 4px 12px rgba(0, 120, 160, 0.1); /* Cyan-blue shadow */
}

.quantity-selector button {
  width: 48px;
  height: 48px;
  border: none;
  background: linear-gradient(135deg, #0078A0 0%, #005c80 100%); /* Cyan-blue gradient */
  color: white;
  cursor: pointer;
  transition: all 0.3s ease;
  font-size: 1.2em;
  font-weight: 600;
}

.quantity-selector button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  background: #ccc;
}

.quantity-selector button:hover:not(:disabled) {
  background: linear-gradient(135deg, #005c80 0%, #004060 100%); /* Darker cyan-blue gradient */
}

.quantity-selector input {
  width: 80px;
  height: 48px;
  border: none;
  text-align: center;
  font-size: 1.2em;
  font-weight: 700;
  color: #0078A0; /* Cyan-blue */
  background: white;
}

.add-to-cart-btn {
  flex: 1;
  height: 48px;
  background: linear-gradient(135deg, #0078A0 0%, #005c80 100%); /* Cyan-blue gradient */
  color: white;
  border: none;
  border-radius: 15px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  font-size: 1.15em;
  font-weight: 700;
  transition: all 0.3s ease;
  box-shadow: 0 6px 15px rgba(0, 120, 160, 0.2); /* Cyan-blue shadow */
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.add-to-cart-btn:disabled {
  background: linear-gradient(135deg, #ccc 0%, #999 100%);
  cursor: not-allowed;
  box-shadow: none;
}

.add-to-cart-btn:not(:disabled):hover {
  background: linear-gradient(135deg, #005c80 0%, #004060 100%); /* Darker cyan-blue gradient */
  transform: translateY(-3px);
  box-shadow: 0 8px 20px rgba(0, 120, 160, 0.3); /* Cyan-blue shadow */
}

.loading {
  text-align: center;
  padding: 60px;
  font-size: 1.3em;
  color: #0078A0; /* Cyan-blue */
  background: rgba(255, 255, 255, 0.95);
  border-radius: 24px;
  box-shadow: 0 15px 40px rgba(0, 120, 160, 0.12); /* Cyan-blue shadow */
  margin: 40px auto;
  max-width: 400px;
  animation: pulse 2s infinite;
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes pulse {
  0% {
    transform: scale(1);
    opacity: 1;
  }
  50% {
    transform: scale(1.02);
    opacity: 0.8;
  }
  100% {
    transform: scale(1);
    opacity: 1;
  }
}

@media (max-width: 768px) {
  .product-detail {
    padding: 20px;
  }

  .product-container {
    grid-template-columns: 1fr;
    gap: 30px;
    padding: 25px;
  }

  .main-image {
    height: 300px;
  }

  .product-header h1 {
    font-size: 1.8em;
  }

  .product-actions {
    flex-direction: column;
    gap: 15px;
    padding: 15px;
  }

  .quantity-selector {
    width: 100%;
  }

  .add-to-cart-btn {
    width: 100%;
    height: 54px;
  }

  .product-price {
    flex-direction: column;
    align-items: flex-start;
    gap: 10px;
  }

  .price-value {
    font-size: 2em;
  }

  .product-specs {
    padding: 20px;
  }

  .specs-grid {
    grid-template-columns: 1fr;
  }
}
</style> 